var CrossFader = Class.create({
	interval: 10, //duration between fades
	items: [],
	visibleItem: null,
	timeoutId: null,

	initialize: function() {
		for (var i = 0 ; i < arguments.length ; i++) {
			var item = {
				container: $(arguments[i]),
				image: $(arguments[i]).down('.image')
			};
			this.items.push(item);
		}
	},

	start: function() {
		if (this.items.length < 2) { return; }
		var _this = this;
		this.timeoutId = setTimeout(function(){_this.crossfade();}, 1000 * this.interval);
	},

	stop: function() {
		clearTimeout(this.timeoutId);
	},

	crossfade: function() {
		//first run, we asume the nr1 container is already showing
		if (this.visibleItem == null) {
			this.visibleItem = this.items[0];
		}

		var idx = this.items.indexOf(this.visibleItem) + 1;
		idx = idx > (this.items.length - 1) ? 0 : idx;

		this.hide(this.visibleItem);
		this.show(this.items[idx]);
		var _this = this;
		this.timeoutId = setTimeout(function(){_this.crossfade();}, 1000 * this.interval);
	},

	hide: function(item) {
		item.container.style.zIndex = 1;
		Effect.Fade(item.container.id);
	},

	show: function(item) {
		item.container.style.zIndex = 2;
		var _this = this;
		var after = function() {
			_this.visibleItem = item;
		};
		Effect.Appear(item.container.id, {afterFinish: after, duration: 1.5});
	}
});